﻿using CallCenter.Data.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Data.BizUtilities
{
   public class ccCallLogs:BaseUtility
    {
         public ccCallLogs(DataManager manager)
            :base(manager)
        { }

            //assign placeholder for the values in the table to insert
            public int Insert(int userId, int companyId, int contactId, int receiverId, int projectId,int communicationId, int solutionId, DateTime callDate, DateTime callStartTime, DateTime callEndTime,string subjectDes, string callStatus, DateTime followupDate, bool serviceCall, string priority,bool billable, string notes, bool visibleToOther)
            {
                int nRtn = -1;
               string sql = "INSERT INTO ccCallsLog" +
                    "(userId, companyId, contactId, receiverID, projectId, communicationId, solutionId, callDate, callStartTime, callEndTime, subjectDes, callStatus, followupDate, serviceCall, priority, billable, notes, visibleToOther)" +
                "VALUES(@userId, @companyId, @contactId, @receiverID, @projectId, @communicationId, @solutionId, @callDate, @callStartTime, @callEndTime, @subjectDes, @callStatus, @followupDate, @serviceCall, @priority, @billable, @notes, @visibleToOther)" +
                "SELECT @ID =@@IDENTITY";
            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@userId", SqlDbType.Int, 10, userId);
                this.Factory.AddParameter(cmd, "@receiverId", SqlDbType.Int, 10, receiverId);
                this.Factory.AddParameter(cmd, "@projectId", SqlDbType.Int, 10, projectId);
                this.Factory.AddParameter(cmd, "@companyId", SqlDbType.Int, 10, companyId);
                this.Factory.AddParameter(cmd, "@contactId", SqlDbType.Int, 10, contactId);
                this.Factory.AddParameter(cmd, "@communicationId", SqlDbType.Int, 10, communicationId);
                this.Factory.AddParameter(cmd, "@solutionId", SqlDbType.Int, 10, solutionId);
                this.Factory.AddParameter(cmd, "@callDate", SqlDbType.SmallDateTime, 0, callDate);
                this.Factory.AddParameter(cmd, "@callStartTime", SqlDbType.SmallDateTime, 0, callStartTime);
                this.Factory.AddParameter(cmd, "@callEndTime", SqlDbType.SmallDateTime, 0, callEndTime);
                this.Factory.AddParameter(cmd, "@visibleToOthers", SqlDbType.Bit, 0, visibleToOther);
                this.Factory.AddParameter(cmd, "@followupDate", SqlDbType.SmallDateTime, 0, followupDate);
                this.Factory.AddParameter(cmd, "@serviceCall", SqlDbType.Bit, 10, serviceCall);
                this.Factory.AddParameter(cmd, "@priority", SqlDbType.NVarChar, 10, priority);
                this.Factory.AddParameter(cmd, "@billable", SqlDbType.Bit, 10, billable);
                this.Factory.AddParameter(cmd, "@notes", SqlDbType.VarChar, 1000, notes);
                this.Factory.AddParameter(cmd, "@subjectDes", SqlDbType.NVarChar, 1000, subjectDes);
                this.Factory.AddParameter(cmd, "@callStatus", SqlDbType.VarChar, 1000, callStatus);
                // this.Factory.AddParameter(cmd, "@Id", SqlDbType.Int, 10,id);
                this.Factory.ExecuteNonQuery(cmd);

            }

            //// returning the data inserted in the data by its new ID
            //cmdInsert.Parameters.Add("@ID", SqlDbType.Int);
            //cmdInsert.Parameters["@ID"].Direction = ParameterDirection.Output;

            return nRtn; //retruning the data stored in the table
        }



     public int Update(int id, int userId, int companyId, int contactId,int receiverId, int projectId, int communicationId,  int solutionId,  DateTime callDate, DateTime callStartTime,  DateTime callEndTime,  string subjectDes, string callStatus,  DateTime followupDate,  bool serviceCall,  string priority, bool billable,string notes, bool visibleToOther )
            {
                int nRtn = -1;
            string sql = "UPDATE ccCallsLog " +
                  " SET userId=@userId, companyId=@companyId, contactId= @contactId, receiverID=@receiverID, " +
                  " projectId=@projectId, communicationId=@communicationId, solutionId=@solutionId, " +
                  " callDate=@callDate, callStartTime=@callStartTime, callEndTime=@callEndTime, subjectDes=@subjectDes, " +
                  " callStatus=@callStatus, followupDate=@followupDate, serviceCall=@serviceCall, priority=@priority, billable=@billable, notes=@notes, visibleToOther=@visibleToOther WHERE id=@id ";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@userId", SqlDbType.Int, 10, userId);
                this.Factory.AddParameter(cmd, "@receiverId", SqlDbType.Int, 10, receiverId);
                this.Factory.AddParameter(cmd, "@projectId", SqlDbType.Int, 10, projectId);
                this.Factory.AddParameter(cmd, "@companyId", SqlDbType.Int, 10, companyId);
                this.Factory.AddParameter(cmd, "@contactId", SqlDbType.Int, 10, contactId);
                this.Factory.AddParameter(cmd, "@communicationId", SqlDbType.Int, 10, communicationId);
                this.Factory.AddParameter(cmd, "@solutionId", SqlDbType.Int, 10, solutionId);
                this.Factory.AddParameter(cmd, "@callDate", SqlDbType.SmallDateTime, 0, callDate);
                this.Factory.AddParameter(cmd, "@callStartTime", SqlDbType.SmallDateTime, 0, callStartTime);
                this.Factory.AddParameter(cmd, "@callEndTime", SqlDbType.SmallDateTime, 0, callEndTime);
                this.Factory.AddParameter(cmd, "@visibleToOthers", SqlDbType.Bit, 0, visibleToOther);
                this.Factory.AddParameter(cmd, "@followupDate", SqlDbType.SmallDateTime, 0, followupDate);
                this.Factory.AddParameter(cmd, "@serviceCall", SqlDbType.Bit, 10, serviceCall);
                this.Factory.AddParameter(cmd, "@priority", SqlDbType.NVarChar, 10, priority);
                this.Factory.AddParameter(cmd, "@billable", SqlDbType.Bit, 10, billable);
                this.Factory.AddParameter(cmd, "@notes", SqlDbType.VarChar, 1000, notes);
                this.Factory.AddParameter(cmd, "@subjectDes", SqlDbType.NVarChar, 1000, subjectDes);
                this.Factory.AddParameter(cmd, "@callStatus", SqlDbType.VarChar, 1000, callStatus);
                // this.Factory.AddParameter(cmd, "@Id", SqlDbType.Int, 10,id);
                this.Factory.ExecuteNonQuery(cmd);

            }

            //cmdInsert.Parameters.Add("@ID", SqlDbType.Int);
            //cmdInsert.Parameters["@ID"].Value = id;
            return nRtn;
               
            }

            public void Delete(int nContactID) //delete the row of data using the id passed to the function
            {
               string sql = "DELETE FROM ccCallsLog " +
                    "WHERE ID = @ID ";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 4,nContactID);
                this.Factory.ExecuteNonQuery(cmd);
            }
        }

            public void DeleteAll() //delete all the rows in the data.
            {
              string sql = "DELETE FROM CallsLog ";
              this.Factory.ExecuteTable(sql);
            }

            public DataTable GetAll()
            {
             string sql= "SELECT * FROM ccCallsLog";
             return Factory.ExecuteTable(sql);
            }
        }

    }

